<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Model\Address;
use App\Model\User_address;
use Illuminate\Http\Request;


/**
 * Class UserAddressController
 * @package App\Http\Controllers\Api
 * @User wuqin
 * @Date 2018/3/30 0030
 * @Time 14:43
 */
class UserAddressController extends Controller
{
    /**
     * @var Address
     */
    private $addressModel;
    /**
     * @var User_address
     */
    private $user_addModel;

    /**
     * UserAddressController constructor.
     * @param Address $address
     * @param User_address $user_address
     */
    function __construct(Address $address, User_address $user_address)
    {
        $this->addressModel = $address;
        $this->user_addModel = $user_address;
    }

    /**
     * 方法 : add_user_address
     * @User wuqin
     * @DateTime 2018-03-30 14:43
     * @param Request $request
     * @return array
     */
    public function add_user_address(Request $request)
    {
        $post = $request->input();
        if (empty($post))
            return return_array(1, '非法操作');

        if (empty($post['province']))
            return return_array(1, "省不得为空");
        $keys = [
            "address" => $post['province'],
            "rank" => 0,
            "parent" => '-1',
        ];
        if (empty($province = $this->addressModel->get_address_by_key($keys)))
            return return_array(1, "非法省份");


        if (empty($post['city']))
            return return_array(1, "市不得为空");
        $keys = [
            "address" => $post['city'],
            "rank" => 1,
            "parent" => $post['province'],
        ];
        if (empty($city = $this->addressModel->get_address_by_key($keys)))
            return return_array(1, "非法市");


        if (empty($post['county']))
            return return_array(1, "县不得为空");
        $keys = [
            "address" => $post['county'],
            "rank" => 2,
            "parent" => $post['city'],
        ];
        if (empty($county = $this->addressModel->get_address_by_key($keys)))
            return return_array(1, "非法县");


        if (empty($post['address_info']))
            return return_array(1, "详细地址不得为空");

        if (empty($post['get_name']))
            return return_array(1, "收件人不得为空");

        if (empty($post['get_phone']))
            return return_array(1, "电话不得为空");

        if (!preg_match("/^1[345678]\d{9}$/", trim($post['get_phone']))) {
            return return_array(1, "电话格式不正确");
        }

        $user = get_session_user();
        if (empty($user))
            return return_array(3, "非法操作");

        //开启事务
        $this->user_addModel->trans_begin();
        $userModel2 = new User_address();
        $res = $userModel2->get_default_user_address_by_user_id($user['user_id']);
        if (empty($res))
            $is_default = 1;
        else {
            if (empty($post['is_default'])) {
                $is_default = 0;
            } else {
                $up_data = reset($res);
                $up_data["is_default"] = 0;
                $abc = $userModel2->find($up_data["user_address_id"]);
                unset($up_data["user_address_id"]);
                unset($up_data["user_id"]);
                if (empty($res_up = $abc->update_user_address($up_data))) {
                    $this->user_addModel->trans_rollback();
                    return return_array(3, "未知错误");
                }
                $is_default = 1;
            }
        }

        $data = [
            "user_id" => $user["user_id"],
            "province" => $province["id"],
            "city" => $city["id"],
            "county" => $county["id"],
            "address" => $province["fullname"] . $city["fullname"] . $county["fullname"] . $post["address_info"],
            "phone" => $post["get_phone"],
            "get_name" => $post["get_name"],
            "is_default" => $is_default,
        ];
        if (empty($res = $this->user_addModel->add_user_address($data))) {
            $this->user_addModel->trans_rollback();
            return return_array(2, "未知错误");
        }
        $this->user_addModel->trans_commit();
        return return_array(0, "OK", $res);
    }

    public function get_user_address(Request $request, User_address $user_address)
    {
        $post = $request->input();
        if (empty($post))
            return return_array(1, '非法操作');

        $user = get_session_user();
        if (empty($user))
            return return_array(3, "非法用户");

        $data = $user_address->get_user_address_by_user_id($user["user_id"]);

        if (empty($data))
            return return_array(2, "数据为空");
        return return_array(0, "获取成功", $data);
    }

    public function delete_user_address(Request $request, User_address $user_address)
    {
        $post = $request->input();
        if (empty($post))
            return return_array(1, '非法操作');

        $user = get_session_user();
        if (empty($user))
            return return_array(3, "非法用户");

        if (empty($post['id']))
            return return_array(1, "非法参数");

        $this->trans_begin();
        $user_address_res = $user_address->find($post["id"]);
        if (empty($user_address_res) || empty($user_address_res->toArray())) {
            $this->trans_rollback();
            return return_array(1, "地址不存在");

        }

        if (!$user_address_res->delete()) {
            $this->trans_rollback();
            return return_array(1, "删除失败");

        }

        if (!$user_address->del_redis('user_address:' . $user_address_res->user_address_id . ':u:' . $user_address_res->user_id . ':is:' . $user_address_res->is_default)) {
            $this->trans_rollback();
            return return_array(2, "删除失败");
        }

        $this->trans_commit();
        return return_array(0, "删除成功");
    }

    /**
     * 方法 : update_user_address
     * @User wuqin
     * @DateTime 2018-03-30 14:43
     * @param Request $request
     * @return array
     */
    public function update_user_address(Request $request)
    {
        $post = $request->input();
        if (empty($post))
            return return_array(1, '非法操作');

        $user = get_session_user();
        if (empty($user))
            return return_array(3, "非法用户");

        if (empty($post['id']))
            return return_array(1, "非法参数");

        return return_array(0, "OK", $post);
    }
}
